import { globalIgnores } from "eslint/config";
import globals from "globals";
import { defineConfigWithVueTs, vueTsConfigs } from "@vue/eslint-config-typescript";
import pluginVue from "eslint-plugin-vue";
import skipFormatting from "@vue/eslint-config-prettier/skip-formatting";
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
import { readFileSync } from "fs";
// 导入TypeScript相关的ESLint插件
import tseslint from "typescript-eslint";

// * 解决自动引入时报错'ref' is not defined.eslint(no-undef)
const __dirname = dirname(fileURLToPath(import.meta.url));
const autoImportConfig = JSON.parse(
  readFileSync(resolve(__dirname, ".eslintrc-auto-import.json"), "utf-8"),
);

export default defineConfigWithVueTs(
  {
    name: "app/files-to-lint",
    files: ["**/*.{ts,mts,tsx,vue}"],
  },
  globalIgnores([
    "**/dist/**",
    "**/dist-ssr/**",
    "**/coverage/**",
    "**/types/global.d.ts", // 忽略全局类型定义文件
  ]),
  pluginVue.configs["flat/essential"],
  vueTsConfigs.recommended,
  skipFormatting,
  {
    files: ["**/*.{ts,d.ts,tsx}"],
    languageOptions: {
      globals: {
        ...globals.browser,
        ...globals.node,
        ...autoImportConfig.globals,
        GetRequire: "readonly",
      },
      parserOptions: { parser: tseslint.parser },
    },
    rules: {
      "@typescript-eslint/no-unused-vars": [
        "warn",
        {
          /* 已使用的参数之前的参数不校验 */
          args: "after-used",
          /* 忽略以_开头的参数 */
          argsIgnorePattern: "^_",
          /* 不检查catch中的错误对象 */
          "caughtErrors": "none",
        },
      ],
      "@typescript-eslint/no-explicit-any": "off",
      "@typescript-eslint/no-unused-expressions": "off",
      "no-undef": "off", // 关闭 ESLint 原生的 no-undef
    },
  },
  {
    files: ["**/*.{js,ts,vue}"],
    languageOptions: {
      globals: {
        ...globals.browser,
        ...globals.node,
        ...autoImportConfig.globals,
      },
    },
    rules: {
      /* 不允许未定义的变量 */
      "no-undef": "error",
      /* 不允许空行 */
      "no-multiple-empty-lines": ["warn", { max: 1 }],
      /* 不允许未使用的变量 */
      "@typescript-eslint/no-unused-vars": [
        "warn",
        {
          /* 已使用的参数之前的参数不校验 */
          args: "after-used",
          /* 忽略以_开头的参数 */
          argsIgnorePattern: "^_",
          /* 不检查catch中的错误对象 */
          "caughtErrors": "none",
        },
      ],
      /* 必须以分号结尾 */
      semi: ["warn", "always"],
      /* 不允许使用var */
      "no-var": "error",
      /* 不允许无用的转义 */
      "no-useless-escape": "off",
      /* 允许在case语句中声明变量 */
      "no-case-declarations": "off",
      /* 允许使用async函数作为Promise的执行器 */
      "no-async-promise-executor": "off",
      /* 允许常量表达式 */
      "no-constant-binary-expression": "off",
      /* 允许文件名称为单个单词 */
      "vue/multi-word-component-names": "off",
      "@typescript-eslint/no-explicit-any": "off",
    },
  },
);
